import { Component,ViewChild, } from '@angular/core';
import { IonicPage, NavController, NavParams, Slides, AlertController, Content} from 'ionic-angular';
//转卖详情
import { ZhuanmaiDetailPage } from '../zhuanmai-detail/zhuanmai-detail';

import { Https } from '../../app/http.service'
/**
 * Generated class for the ZhuanmaiPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-zhuanmai',
  templateUrl: 'zhuanmai.html',
})
export class ZhuanmaiPage {
	@ViewChild(Slides) slides: Slides;
	@ViewChild(Content) content: Content;
	myZmUrl = new Https().Url+"act=goods&op=app_app_zhuanmai_goods"
	petArray = ['kittens', 'puppies'];
	pet: string = "kittens";
	//转卖中
	data: string = '0';
	carry = [];
	//已转卖
	zhuan: string = '0';
	complate = [];
	constructor(public navCtrl: NavController, public navParams: NavParams, public alertCtrl: AlertController) {
	}

	ionViewDidLoad() {
		console.log('ionViewDidLoad ZhuanmaiPage');
		this.loadData(0);
	}
	loadData(int){
		let then = this
		fetch(this.myZmUrl, {
			method: "post",
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded'
			},
			body: "mid=" + localStorage.getItem("mid") + "&token=" + localStorage.getItem("token"),
		}).then((response) => {
			response.text().then((con) => {
				if (con != '') {
					if(int == 0){
						this.complate = null;
						this.carry = null;
					}
					
					let resource = JSON.parse(con).datas;
					if (resource.carry == 0 && resource.complate == 0) {
						this.data = '0';
						this.zhuan = '0';
						console.log(0)
					} else if (resource.carry == 0) {
						this.data = '0';
						this.zhuan = '1';
						this.complate = resource.complate;
						console.log(this.complate)
					} else if (resource.complate == 0) {
						this.zhuan = '0';
						this.data = '1';
						this.carry = resource.carry;
						console.log(this.carry)
					} else {
						this.carry = resource.carry;
						this.complate = resource.complate;
						this.zhuan = '1';
						this.data = '1';
						console.log(1)
					}
				} else {
					this.data = '0';
					this.zhuan = '0';
					then.xalert("网络连接错误");
				}


			})
		}).catch((err) => {
			then.xalert(err);
		})
	}
	/* 
		slide改变index触发
	*/
	slideChanged(event,zhong) {
		//获取当前页面index
		this.loadData(0)
		let currentIndex = this.slides.getActiveIndex() > 1 ? 1 : this.slides.getActiveIndex();
		//回到顶部
		this.content.scrollTo(0, 0, 0); 
		//定位segment
		this.pet = this.petArray[currentIndex];
		//event.container : 高度
		//获取slide子元素
		let brother = event.container.children[0].children;

		for (let i = 0; i < brother.length; i++) {
			if (i != currentIndex) {
				if (brother[i].children[0].children[0].offsetHeight > brother[currentIndex].children[0].children[0].offsetHeight) {
					brother[i].children[0].children[0].style = "height:" + brother[currentIndex].children[0].children[0].offsetHeight + "px"
				}
			} else {
				brother[i].children[0].children[0].style = "height:unset"
			}

		}

	}
	/* 
		点击tab定位
	*/
	swipeEvnet(event) {
		//跳转到指定页面
		//获取ID定位
		console.log(this.petArray.indexOf(event.target.id))
		this.slides.slideTo(this.petArray.indexOf(event.target.id));
	}
	orderDetail(Id){
		this.navCtrl.push(ZhuanmaiDetailPage,{
			"id":Id
		})
	}

	doInfinite(event){

		console.log("加载" );
		event.complete();
	}

	xalert(text){
		let confirm = this.alertCtrl.create({
			title: '温馨提示',
			message: text,
			buttons: [
				{ text: '确定' },

			]
		});
		confirm.present();
	}
}
